package com.corgi.mybatis.config;

import com.corgi.mybatis.interceptor.mybatis.executor.LogInterceptor;
import com.corgi.mybatis.interceptor.mybatis.executor.PageInterceptor;
import com.corgi.mybatis.interceptor.mybatis.result.ResultEncryptInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @title: MybatisConfiguration
 * @description:
 * @author: dengmiao
 * @create: 2019-10-18 11:27
 **/
@Configuration
public class MybatisConfiguration {

    @Bean
    public LogInterceptor logInterceptor() {
        LogInterceptor interceptor = new LogInterceptor();
        Properties properties = new Properties();
        // 设置是否显示数据库执行语句的执行时间
        properties.setProperty(LogInterceptor.PROPERTIES_KEY_ENABLE_EXECUTOR_TIME, LogInterceptor.ENABLE_EXECUTOR_TIME);
        interceptor.setProperties(properties);
        return interceptor;
    }

    @Bean
    public PageInterceptor pageInterceptor() {
        PageInterceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty(PageInterceptor.PROPERTIES_KEY_DATABASE_DIALECT, PageInterceptor.DATABASE_DIALECT_MYSQL);
        properties.setProperty(PageInterceptor.PROPERTIES_KEY_PAGE_EXPRESSION_REGEX, ".*(ListPage|PageList).*");
        // Count
        properties.setProperty(PageInterceptor.PROPERTIES_KEY_COUNT_SUFFIX, "");
        interceptor.setProperties(properties);
        return interceptor;
    }

    @Bean
    public ResultEncryptInterceptor encryptResultFieldInterceptor() {
        return new ResultEncryptInterceptor();
    }
}
